//
//  SecretaryViewController.m
//  安好
//
//  Created by qinlongqing on 14/12/10.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "SecretaryViewController.h"
#import "SecretaryTableViewCell.h"
#import "SystemSecretaryDetailViewController.h"
#import "SecretaryModel.h"
#import "SystemMessageCenter.h"

@interface SecretaryViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *myTableView;
    
    NSArray *allSystemMessageModelArray;
}
@end

@implementation SecretaryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    NSString *dbPath = [NSString stringWithFormat:@"%@/Library/Caches/tijianTest.db",NSHomeDirectory()];
//    NSLog(@"----%@",dbPath);
    
    [self makeNavBar];
    
    self.view.backgroundColor = UIColorFromRGB(0xf2f2f2);
    
    allSystemMessageModelArray = [[SystemMessageCenter instance] getAllSystemMessage];
    
    [self createTableView];
}

-(void)makeNavBar{
    self.navigationItem.title = @"安好秘书";
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnclicked)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
}

- (void)backBtnclicked{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createTableView{
    myTableView = [[UITableView alloc]initWithFrame:CGRectMake(15, 0, kScreenWidth - 30, kScreenHeight - 64) style:UITableViewStylePlain];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    myTableView.backgroundColor = [UIColor clearColor];
    myTableView.showsHorizontalScrollIndicator = NO;
    myTableView.showsVerticalScrollIndicator = NO;
//    myTableView.alwaysBounceHorizontal = NO;
//    myTableView.alwaysBounceVertical = NO;
    myTableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
    if (kScreenWidth == 320) {
        myTableView.contentOffset = CGPointMake(0, 280*allSystemMessageModelArray.count);
    }
    else if (kScreenWidth == 375){
        myTableView.contentOffset = CGPointMake(0, 301*allSystemMessageModelArray.count);
    }
    else if (kScreenWidth == 414){
        myTableView.contentOffset = CGPointMake(0, 336*allSystemMessageModelArray.count);
    }
    [self.view addSubview:myTableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return allSystemMessageModelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (kScreenWidth == 320) {
        return 280;
    }
    else if (kScreenWidth == 375){
        return 301;
    }
    else if (kScreenWidth == 414){
        return 336;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"secretaryCell";
    SecretaryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        if (kScreenWidth == 414) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"SecretaryTableViewCell" owner:self options:nil] lastObject];
        }
        else{
            cell = [[[NSBundle mainBundle] loadNibNamed:@"SecretaryTableViewCell" owner:self options:nil] firstObject];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.detailBTN.tag = indexPath.row + 1;
    SecretaryModel *model = [allSystemMessageModelArray objectAtIndex:indexPath.row];
    cell.titleLabel.text = model.title;
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:model.dateline];
    NSDateFormatter *formatter =  [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/beijing"];
    [formatter setTimeZone:timeZone];
    NSString *loctime = [formatter stringFromDate:date];
    cell.dateLabel.text = loctime;
    
    if (model.pmsid == -1) {
        cell.pictureImageView.image = [UIImage imageWithContentsOfFile:model.pic];
    }
    else{
        [cell.pictureImageView setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:[UIImage imageNamed:@"秘书默认"]];
    }
    
    cell.pictureImageView.layer.cornerRadius = 3;
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:model.content];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];//调整行间距
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [model.content length])];
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    cell.detailDescriptionLabel.attributedText = attributedString;
    
    [cell.detailBTN addTarget:self action:@selector(doShowSecretaryDetail:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *url = ((SecretaryModel *)[allSystemMessageModelArray objectAtIndex:indexPath.row]).url;
    NSString *title = ((SecretaryModel *)[allSystemMessageModelArray objectAtIndex:indexPath.row]).title;
    SystemSecretaryDetailViewController *detail = [[SystemSecretaryDetailViewController alloc]init];
    detail.url = url;
    detail.titleString = title;
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)doShowSecretaryDetail:(UIButton *)sender{
    NSString *url = ((SecretaryModel *)[allSystemMessageModelArray objectAtIndex:(sender.tag - 1)]).url;
    NSString *title = ((SecretaryModel *)[allSystemMessageModelArray objectAtIndex:(sender.tag - 1)]).title;
    SystemSecretaryDetailViewController *detail = [[SystemSecretaryDetailViewController alloc]init];
    detail.url = url;
    detail.titleString = title;
    [self.navigationController pushViewController:detail animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
